
package com.yimixian.app.user;

import com.squareup.picasso.Picasso;
import com.yimixian.app.R;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.AppConfig;
import com.yimixian.app.model.Entry;
import com.yimixian.app.util.StringUtils;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class SettingItemView extends FrameLayout {

    @InjectView(R.id.iv_icon)
    ImageView mIvIcon;
    @InjectView(R.id.tv_content)
    TextView mTvContent;
    @InjectView(R.id.right_icon)
    ImageView mRightIcon;
    @InjectView(R.id.tv_tel)
    TextView mTvTel;

    public SettingItemView(Context context) {
        super(context);
        initGoodsView();
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initGoodsView();
    }

    public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initGoodsView();
    }

    private void initGoodsView() {
        LayoutInflater.from(getContext()).inflate(R.layout.setting_item_view, this);
        ButterKnife.inject(this);
    }

    public void bind(int icon, String content,boolean isShowText) {
        mIvIcon.setImageResource(icon);
        mTvContent.setText(content);
        mRightIcon.setImageResource(R.drawable.ic_arrow_right);
        if(isShowText) {
            mTvTel.setVisibility(View.VISIBLE);
            mRightIcon.setVisibility(View.GONE);
        } else {
            mRightIcon.setVisibility(View.VISIBLE);
            mTvTel.setVisibility(View.GONE);
        }
    }

    public void bind(Entry each) {

        String title = each.title;
        String title_color = each.title_color;
        String sub_title = each.sub_title;
        String sub_title_color = each.sub_title_color;
        String url = each.url;
        String shows_indicator = each.shows_indicator;

        Picasso.with(getContext()).load(DataManager.getInstance().getImageUrl(each.img,
                (int) (getContext().getResources().getDisplayMetrics().density * 18)))
                .placeholder(R.drawable.ic_setting_setting).into(mIvIcon);
        mTvContent.setText(title);
        if(!StringUtils.isNullOrEmpty(title_color)){
            mTvContent.setTextColor(Color.parseColor("#" + title_color));
        }
        if(!StringUtils.isNullOrEmpty(sub_title)){
            mTvTel.setText(sub_title);
            mTvTel.setVisibility(View.VISIBLE);
        }
        if(!StringUtils.isNullOrEmpty(sub_title_color)){
            mTvTel.setTextColor(Color.parseColor("#" + sub_title_color));
        }
        mRightIcon.setImageResource(R.drawable.ic_arrow_right);
        mRightIcon.setVisibility("true".equalsIgnoreCase(shows_indicator) ? View.VISIBLE : View.GONE);

    }

    public void setTextColor(int color){
        if (mTvContent != null) mTvContent.setTextColor(color);
    }

}
